﻿using System;
using System.Device.Location;
using System.Threading;

namespace Tequila.Phone.Location
{
    public class GpsLocator : IGpsLocator
    {
        private const int StartTimeout = 30000;

        public void GetUserLocation(Action<GeoCoordinate> callback)
        {
            ThreadPool.QueueUserWorkItem(waitCallback => TryGetUserLocation(callback));
        }

        private void TryGetUserLocation(Action<GeoCoordinate> callback)
        {
            using (var watcher = new GeoCoordinateWatcher())
            {
                watcher.TryStart(false, TimeSpan.FromMilliseconds(StartTimeout));
                if (callback != null)
                {
                    callback(watcher.Position.Location);
                }
            }
        }
    }
}